package com.wwp.tbshop.model.adapter;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.LimitBuyActivity;
import com.wwp.tbshop.model.bean.LimitBuyBean;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;
import com.wwp.tbshop.util.utils.TimeTools;

import java.util.List;

/**
 * Created by ${wenping} on 6/19/2017.
 */

public class ListViewAdapter extends BaseAdapter {

    private LimitBuyActivity mLimitBuyActivity;
    private List<LimitBuyBean.ProductListBean> list;

    public void setList(List<LimitBuyBean.ProductListBean> list) {

        this.list = list;
    }

    public ListViewAdapter(LimitBuyActivity limitBuyActivity) {
        mLimitBuyActivity = limitBuyActivity;
    }

    @Override
    public int getCount() {
        if (list==null){
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
        convertView = View.inflate(mLimitBuyActivity, R.layout.item_limit_buy, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
            viewHolder.setData(list.get(position));
        return convertView;
    }

    static class ViewHolder {

        public CountDownTimer countDownTimer;
        ImageView mItemLimitBuyGoodsiv;
        TextView mItemLimitBuyName;
        TextView mItemLimitBuyPrice;
        TextView mItemLimitBuyMarketprice;
        TextView mItemLimitBuyLeftTime;

        public ViewHolder(View view) {
            mItemLimitBuyGoodsiv = (ImageView) view.findViewById(R.id.item_limit_buy_goodsiv);
            mItemLimitBuyName = (TextView) view.findViewById(R.id.item_limit_buy_name);
            mItemLimitBuyPrice = (TextView) view.findViewById(R.id.item_limit_buy_price);
            mItemLimitBuyMarketprice = (TextView) view.findViewById(R.id.item_limit_buy_marketprice);
            mItemLimitBuyLeftTime = (TextView) view.findViewById(R.id.item_limit_buy_lefttime);
        }

        public void setData(LimitBuyBean.ProductListBean bean) {
            Picasso.with(UiUtil.getContext())
                    .load(Constant.HOST + bean.getPic())
                    .placeholder(R.drawable.pic_default_grid)
                    .error(R.drawable.ic_error_page)
                    .into(mItemLimitBuyGoodsiv);
            mItemLimitBuyName.setText(bean.getName() + "");
            mItemLimitBuyMarketprice.setText("¥" + bean.getPrice() + "");
            mItemLimitBuyMarketprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//贯穿线
            mItemLimitBuyPrice.setTextColor(Color.RED);
            mItemLimitBuyPrice.setText("¥" + bean.getLimitPrice() + "");

            final int time = bean.getLeftTime();
            if (time>0){
                countDownTimer = new CountDownTimer(time*1000,1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        mItemLimitBuyLeftTime.setText(TimeTools.getCountTimeByLong(((int) millisUntilFinished)));
                    }

                    @Override
                    public void onFinish() {
                        mItemLimitBuyLeftTime.setText("00:00:00");
                    }
                }.start();
            }else {
                mItemLimitBuyLeftTime.setText("00:00:00");
            }
        }
    }
}
